package com.yl.auth.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.model.entity.Yuangong;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yl.auth.model.vo.YuaGongPaiBanVo;
import com.yl.auth.model.vo.YuangongQueryVo;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author
 * @since 2023-06-15
 */
public interface YuangongMapper extends BaseMapper<Yuangong> {
    List<Yuangong> findAll(@Param("yuangong") YuangongQueryVo yuangong);

    IPage<Yuangong> findAllhugong(Page<Yuangong> page1, @Param("yuangong") Yuangong yuangong);

    @Select("SELECT y.id,y.avatar avatar,y.`name` yname,phone,o.name oname,COUNT(o.`name`) laorencount,GROUP_CONCAT(g.`name`) gname from yuangong y\n" +
            "           LEFT JOIN hugong_laoren z on y.id = z.yid\n" +
            "         LEFT JOIN laoren o on z.lid = o.id \n" +
            "           left join yuangong_gangwei yg on yg.yuangongid=y.id\n" +
            "           left join gangwei g on yg.gangweiid=g.id\n" +
            "           where y.hugongstatus=1 and y.id=#{id} and o.`status`=2")
    Yuangong selectByIdHugong(@Param("id") Integer id);

    @Update("update yuangong set hugongstatus=1 where id=#{id}")
    void updateyuangong(@Param("id") Long id);

    List<Yuangong> listPaiBan(@Param("vo") YuaGongPaiBanVo yuaGongPaiBanVo);

    @Update("update yuangong set hugongstatus=0 where id=#{id}")
    void updateyuan(Integer id);

    IPage<Yuangong> getygList(@Param("page") Page<Yuangong> page, @Param("yuangong") Yuangong yuangong);

    @Select("select * from yuangong where name = #{name}")
    Yuangong getByName(@Param("name") String name);

    List<Yuangong> find(@Param("yuangong") YuangongQueryVo yuangong);

    Page<Yuangong> getAllAndPage(Page<Yuangong> page1, @Param("yuangong") Yuangong yuangong);

    Yuangong getbyId(@Param("id") Long id);

    List<Integer> getBuMenById(@Param("id") Integer id);

    List<Integer> getGangWeiById(@Param("usersId") Integer usersId);

    @Insert("INSERT INTO yuangong_bumen(yuangongid,bumenid) VALUES(#{id},#{bumenid})")
    void addCenterBuMen(@Param("id") Long id, @Param("bumenid") Integer bumenid);

    @Insert("INSERT INTO user_role(user_id,role_id) VALUES(#{id},#{gangweiid})")
    void addCenterGangWei(@Param("id") Long id, @Param("gangweiid") Integer gangweiid);

    @Delete("DELETE from user_role where user_id=#{id}")
    void deleteGangWei(@Param("id")Long id);

    @Delete("DELETE from yuangong_bumen where yuangongid=#{id}")
    void deleteBuMen(@Param("id") Long id);

    @Insert("insert into hugong_laoren(lid,yid)\n" +
            "values(#{id1},#{id})")
    void savelaoren(@Param("id1") Integer id1, @Param("id") Long id);
    @Select("select * from yuangong where account=#{yuangong.account} and password=#{yuangong.password}")
    Yuangong login(Yuangong yuangong);
    @Select("select * from yuangong where account=#{account} and password=#{password}")
    Yuangong login(@Param("account") String account, @Param("password") String password);
    @Update("update yuangong set password=#{yuangong.password} where id=#{yuangong.id}")
    void updatePassWord(@Param("yuangong") Yuangong yuangong);

    @Insert("INSERT INTO yuangongxinzi(yid,money,endTime) VALUES(#{yuangong.id},#{yuangong.money},now())")
    void insertXinzi(@Param("yuangong") Yuangong yuangong);
    @Delete("DELETE from yuangongxinzi where yid=#{id}")
    void removeXinZi(Long id);
    @Delete("DELETE from users where id=#{yuangong.usersId}")
    void removeUsers(@Param("yuangong") Yuangong yuangong);


}
